package com.project.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.project.entity.Contact;
import com.project.entity.Contactnote;
import com.project.entity.User;
import com.project.util.DBHelp;
import com.project.util.RowMapper;

public class NoteDao {
	DBHelp<Contactnote> dbHelp=new DBHelp<Contactnote>();
 	public Contactnote findNoteByContactid(String cid,String userid) {
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT t_contactnote.id,`name`,t_contactnote.cont,t_contactnote.createtime,userid,contactid,t_user.username AS username FROM t_contactnote   ");
		sql.append("LEFT JOIN t_user ON ?=t_user.id   ");
		sql.append("LEFT JOIN t_contact ON t_contact.id=t_contactnote.contactid  ");
		sql.append("WHERE contactid=? order by t_contactnote.createtime desc");
		return dbHelp.executeQueryForObject(sql.toString(), new ContactnoteROWMapper(), userid,cid) ;
	}

 	
 	private class ContactnoteROWMapper implements RowMapper<Contactnote>{

		public Contactnote mapRow(ResultSet rs) throws SQLException {
			
			Contactnote cn = new Contactnote();
			cn.setCreatetime(rs.getString("createtime"));
			cn.setId(rs.getString("id"));
			cn.setCont(rs.getString("cont"));
			cn.setContactid(rs.getString("contactid"));
			
			
			User user=new User();
			user.setId(rs.getString("userid"));
			user.setUsername(rs.getString("username"));
			

			Contact c= new Contact();
			c.setId(rs.getString("contactid"));
			c.setName(rs.getString("name"));
			
			
			cn.setUser(user);
			cn.setContact(c);
			
			return cn;
		}
		
	}


	public List<Contactnote> findAllNoteByContactid(String cid) {
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT t_contactnote.id,`name`,t_contactnote.cont,t_contactnote.createtime,userid,contactid,t_user.username AS username FROM t_contactnote   ");
		sql.append("LEFT JOIN t_user ON t_contactnote.userid=t_user.id   ");
		sql.append("LEFT JOIN t_contact ON t_contact.id=t_contactnote.contactid  ");
		sql.append("WHERE contactid=? order by t_contactnote.createtime desc");

		return dbHelp.executeQueryForList(sql.toString(), new ContactnoteROWMapper(), cid);
	}


	public void save(Contactnote cn) {
		String sql="insert into t_contactnote(id,createtime,cont,userid,contactid) values(?,?,?,?,?) ";
		dbHelp.executeSQL(sql, cn.getId(),cn.getCreatetime(),cn.getCont(),cn.getUserid(),cn.getContactid());
		
	}


	public List<Contactnote> findAllNoteByUserid(String userid) {
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT t_contactnote.id,`name`,t_contactnote.cont,t_contactnote.createtime,userid,contactid,t_user.username AS username FROM t_contactnote   ");
		sql.append("LEFT JOIN t_user ON t_contactnote.userid=t_user.id   ");
		sql.append("LEFT JOIN t_contact ON t_contact.id=t_contactnote.contactid  ");
		sql.append("WHERE userid=? order by t_contactnote.createtime desc");

		return dbHelp.executeQueryForList(sql.toString(), new ContactnoteROWMapper(), userid);
	}
 	
 	
 	
 	
 	
}
